<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>HOJ后台管理</title>
  <link rel="stylesheet" href="./layui/css/layui.css">
  <script src="./layui/layui.js"></script>
  <script src="../js/jquery-3.3.1.min.js"></script>
  <script src="../js/jquery.form.min.js"></script>
  <!-- editor.md的css文件 -->
  <link rel="stylesheet" href="../plugs/editormd/css/editormd.css" />
  <link rel="stylesheet" href="../plugs/editormd/css/editormd.preview.min.css" />
  <!-- editor.md的js文件 -->
  <script src="../plugs/editormd/lib/marked.min.js"></script>
  <script src="../plugs/editormd/lib/prettify.min.js"></script>
  <script src="../plugs/editormd/lib/raphael.min.js"></script>
  <script src="../plugs/editormd/lib/underscore.min.js"></script>
  <script src="../plugs/editormd/editormd.js"></script>
  <script src="../plugs/editormd/plugins/image-dialog-qiniu/image-dialog-qiniu.js"></script>
  <!-- codemirror格式文件 -->
  <link rel="stylesheet" href="../plugs/codemirror/lib/codemirror.css" />
  <link rel="stylesheet" href="../plugs/codemirror/theme/seti.css" />
  <link rel="stylesheet" href="../plugs/codemirror/addon/fold/foldgutter.css" />
  <!-- codemirror js文件 -->
  <script src="../plugs/codemirror/lib/codemirror.js"></script>
  <script src="../plugs/codemirror/mode/clike/clike.js"></script>
  <script src="../plugs/codemirror/addon/edit/matchbrackets.js"></script>
  <script src="../plugs/codemirror/addon/edit/closebrackets.js"></script>
  <!-- 代码折叠 -->
  <script src="../plugs/codemirror/addon/fold/foldcode.js"></script>
  <script src="../plugs/codemirror/addon/fold/foldgutter.js"></script>
  <script src="../plugs/codemirror/addon/fold/brace-fold.js"></script>
  <script src="./js/controller.js"></script>
  <script src="./js/myajax.js"></script>
  <script src="./js/filter.js"></script>
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
  <div class="layui-header">
    <div class="layui-logo">HOJ后台管理</div>
    <!-- 头部区域（可配合layui已有的水平导航） -->
    <ul class="layui-nav layui-layout-right">
        <li class="layui-nav-item"><a href="/front/index">返回主页</a></li>
    </ul>
  </div>
  
  <div class="layui-side layui-bg-black">
    <div class="layui-side-scroll">
      <!-- 左侧导航区域（可配合layui已有的垂直导航） -->
      <ul class="layui-nav layui-nav-tree"  lay-filter="test">
        <li id="tab1" class="layui-nav-item">
          <a href="javascript:;">页面管理</a>
          <dl class="layui-nav-child">
            <dd id="tab1-1"><a href="./index">主页信息</a></dd>
            <dd><a href="javascript:;">bala bala</a></dd>
          </dl>
        </li>
        <li id="tab2" class="layui-nav-item">
          <a href="javascript:;">用户管理</a>
          <dl class="layui-nav-child">
            <dd id="tab2-1"><a href="./userList">用户概览</a></dd>
            <dd id="tab2-2"><a href="./code">代码查看</a></dd>
          </dl>
        </li>
        <li id="tab3" class="layui-nav-item">
          <a href="javascript:;">题目管理</a>
          <dl class="layui-nav-child">
            <dd id="tab3-1"><a href="./problemList">题目概览</a></dd>
            <dd id="tab3-2"><a href="./addProblem">添加题目</a></dd>
          </dl>
        </li>
        <li id="tab4" class="layui-nav-item">
          <a href="javascript:;">比赛管理</a>
          <dl class="layui-nav-child">
            <dd id="tab4-1"><a href="./contestList">比赛概览</a></dd>
            <dd id="tab4-2"><a href="./addContest">添加比赛</a></dd>
            <dd id="tab4-3"><a href="./contestUser">用户总览</a></dd>
          </dl>
        </li>
      </ul>
    </div>
  </div>
  
  <div class="layui-body">
    <!-- 内容主体区域 -->
    <div class="layui-row">
      <div class="layui-col-md9" style="margin-left: 1em; margin-top: 1em">
        <span class="layui-breadcrumb" style="font-size: 2em">
          <a id="menu1" href="javascript:;"></a>
          <a id="menu2" href="javascript:;"></a>
          <a id="menu3" href="javascript:;"></a>
        </span>
      </div>
    </div>
    <div style="padding: 15px;">
      <div class="layui-row">
        <!-- Column -->
        <div class="layui-row" id="div1">
            <div class="layui-col-md6 layui-col-md-offset3">
                <form class="layui-form layui-form-pane" role="form" id="problemInfo">

                    <div class="layui-form-item">
                        <label for="title" class="layui-form-label">题目标题</label>
                        <div class="layui-input-block">
                            <input type="text" class="layui-input" id="title" name="title"/>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label for="timeLimit" class="layui-form-label">题目时限</label>
                        <div class="layui-input-inline">
                            <input type="number" class="layui-input" id="timeLimit" name="timeLimit"/>
                        </div>
                        <div class="layui-form-mid layui-word-aux">
                            <span>(单位: <strong>秒</strong>)</span>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label for="timeLimitSpc" class="layui-form-label">特别时限</label>
                        <div class="layui-input-inline">
                            <input type="number" class="layui-input" id="timeLimitSpc" name="timeLimitSpc"/>
                        </div>
                        <div class="layui-form-mid layui-word-aux">
                            <span>(单位: <strong>秒</strong>)</span>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label for="memoryLimit" class="layui-form-label">内存限制</label>
                        <div class="layui-input-inline">
                            <input type="number" class="layui-input" id="memoryLimit" name="memoryLimit"/>
                        </div>
                        <div class="layui-form-mid layui-word-aux">
                            <span>(单位: <strong>M</strong> 字节)</span>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label for="author" class="layui-form-label">题目作者</label>
                        <div class="layui-input-block">
                            <input type="text" class="layui-input" id="author" name="author"/>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label for="source" class="layui-form-label">题目来源</label>
                        <div class="layui-input-block">
                            <input type="text" class="layui-input" id="source" name="source"/>
                        </div>
                    </div>

                    <div class="layui-form-item">
                      <label for="docType" class="layui-form-label">题面类型</label>
                      <div class="layui-input-inline">
                        <select id="docType" name="docType">
                          <option value="pdf">pdf</option>
                          <option value="md" selected>markdown</option>
                          <option value="html">html</option>
                        </select>
                      </div>
                      <div class="layui-form-mid layui-word-aux">
                        <span>推荐md格式</span>
                      </div>
                    </div>

                    <div class="layui-form-item">
                      <label for="isSpj" class="layui-form-label">判题方式</label>
                      <div class="layui-input-inline">
                        <select id="isSpj" name="isSpj">
                          <option value="1">特判程序</option>
                          <option value="0" selected>评测数据</option>
                        </select>
                      </div>
                    </div>

                    <div class="layui-form-item">
                      <label for="judgeFiles" class="layui-form-label">评测组数</label>
                      <div class="layui-input-inline">
                          <input type="number" class="layui-input" id="judgeFiles" name="judgeFiles" placeholder="评测数据组数" />
                      </div>
                  </div>

                    <div class="layui-form-item">
                      <input type="button" class="layui-btn" id="infoSubmit" value="提交" />
                    </div>
                </form>
            </div>
        </div>

        <div class="layui-row" id="div2" style="height: 52em">
          <div id="docUpload" class="col-md-12 row"></div>
          <div class="layui-col-md3 layui-col-md-offset5">
            <button type="button" class="layui-btn" id="docSubmit" style="margin-top: 1em">提交</button>
          </div>
        </div>

        <div class="col-md-12 row" id="div3" style="height: 52em">
            <div id="fileUpload" class="col-md-12 row" style="margin-bottom: 2em;"></div>
            <div class="layui-col-md3 layui-col-md-offset5">
                <button type="button" class="layui-btn" id="dataSubmit" style="margin-top: 1em">提交</button>
              </div>
        </div>

<!-- 
        <div class="col-md-12 row" style="margin-top: 3em">
            <div class="col-md-2 column"></div>
            <div class="col-md-8 column">
                <button id="pre" type="button" class="btn btn-primary" style="width: 10em"><i class="fa fa-arrow-left m-r-10" aria-hidden="true"></i>previous</button>
                <button id="next" type="button" class="btn btn-primary pull-right" style="width: 10em">next<i class="fa fa-arrow-right m-l-10"></i></button>
            </div>
        </div> -->
      </div>
    </div>
  </div>
  
  <div class="layui-footer">
    <!-- 底部固定区域 -->
    © Hunan University Online Judge
  </div>
</div>
<script src="./layui/layui.js"></script>
<script>
  //JavaScript代码区域
  layui.use('element', function(){
    var element = layui.element;
  });
  layui.use('form', function(){
    var form = layui.form;
    form.render();
  })
  var tab = document.getElementById("tab3");
  tab.className += "  layui-nav-itemed";
  var tab_t = document.getElementById("tab3-2");
  tab_t.className += " layui-this";


  //几个重要变量
  var isSpj = '-1';
  var docType = "";
  var testEditor = null; //md题面编辑器
  var editor = null;     //spj源码编辑器
  var judgeFiles = "0";
  var problemId = 0;

  document.getElementById("infoSubmit").onclick = function() {
    var jsonData = new Object();
    jsonData.title = document.getElementById("title").value;
    jsonData.timeLimit = document.getElementById("timeLimit").value;
    jsonData.timeLimitSpc = document.getElementById("timeLimitSpc").value;
    jsonData.memoryLimit = document.getElementById("memoryLimit").value;
    jsonData.author = document.getElementById("author").value;
    jsonData.source = document.getElementById("source").value;
    jsonData.docType = document.getElementById("docType").value;
    jsonData.isSpj = String(document.getElementById("isSpj").value);
    jsonData.judgeFiles = String(document.getElementById("judgeFiles").value == "" ? 0 : document.getElementById("judgeFiles").value);
    var data = new Object();
    data.data = jsonData;
    $.ajax({
      type: "POST",
      url : ojurl + "/admin/problem/add",
      dataType: "json",
      contentType: "application/json",
      timeout: 10000,
      data: JSON.stringify(data),
      beforeSend: function(request) {
        appendToken(request);
      },
      success : function(result) {
        if (result.code == HttpStatus.SUCCESS) {
          problemId = result.data.problemId;
          docType = jsonData.docType;
          isSpj = jsonData.isSpj;
          judgeFiles = jsonData.judgeFiles;
          alert("基本信息上传成功");
          showDIV(2);
        }
        else {
          alert(result.message);
        }
      }
    })
  };
  document.getElementById("docSubmit").onclick = function() {
    if (docType == "pdf" || docType == "html") {
      var formData = new FormData(document.getElementById("problemUpload"));
      formData.append("problemId", String(problemId));
      formData.append("docType", docType);
      $.ajax({
        type : "POST",
        url : ojurl + "/admin/problem/doc",
        beforeSend: function(request) {
          appendToken(request);
        },
        data: formData,
        cache: false,
        contentType: false,
        processData: false,
        timeout: 10000,
        success: function(result) {
          if (result.code == HttpStatus.SUCCESS) {
            alert("上传成功");
            showDIV(3);
          }
          else {
            alert(result.message);
          }
        }
      })
    }
    else if(docType == "md") {
      var data = new Object();
      data.problemId = problemId;
      data.docType = docType;
      data.problemContent = testEditor.getMarkdown();
      $.ajax({
        type: "POST",
        url: ojurl + "/admin/problem/text",
        timeout: 10000,
        data: data,
        beforeSend: function(request) {
          appendToken(request);
        },
        success: function(result) {
          if (result.code == HttpStatus.SUCCESS) {
            alert("上传成功");
            showDIV(3);
          }
          else {
            alert(result.message);
          }
        }
      })
    }
  };
  document.getElementById("dataSubmit").onclick = function() {
    if (isSpj == '0') {
      var formData = new FormData(document.getElementById("dataUpload"));
      formData.append("problemId", String(problemId));
      formData.append("judgeFiles", String(judgeFiles));
      $.ajax({
        url      : ojurl + "/admin/problem/data",
        type     : "post",
        timeout  : 600000,
        data     : formData,
        cache: false,
        contentType: false,
        processData: false,
        beforeSend : function(request){
          appendToken(request);
        },
        success  : function(result){
            if (result.code == HttpStatus.SUCCESS) {
              alert("上传成功");
              location.href = "./problemList";
            }
            else {
                alert(result.message);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          if (textStatus == 'timeout') {
            alert("上传数据超时");
          }
        }
      })
    }
    else {
      var formData = new FormData(document.getElementById("dataUpload"));
      formData.append("problemId", String(problemId));
      formData.append("judgeFiles", String(judgeFiles));
      var code = editor.getValue();
      var jsonData = new Object();
      jsonData.code = code;
      jsonData.problemId = String(problemId);
      spjUpload(formData, jsonData);
    }
  };

  var menu = ['', '基本信息', '上传题面', '评测数据'];
  var nowDiv = 1;
  function showDIV(arg) {
    nowDiv = arg;
    for (var i=1; i<=3; ++i) {
        var divId = "div" + String(i);
        var menuId = "menu" + String(i);
        if (i == arg) {
            document.getElementById(divId).style.display = "";
            document.getElementById(menuId).innerHTML = '<cite>' + menu[i] + '</cite>';
        }
        else {
            document.getElementById(divId).style.display = "none";
            document.getElementById(menuId).innerHTML = menu[i];
        }
    }
    if (arg == 2) { //第二版面
      var initContent = "";
      if (docType == "md") {
        $.ajax({
            type: "get",
            url: "../data/template.md",
            async: false,
            success: function(result) {
              initContent = result;
            }
          })
      }
      problem_DIV2(docType, initContent);
    }
    if (arg == 3) {  //第三版面
      problem_DIV3(isSpj);
    }   
  }
  
  showDIV(nowDiv);
</script>
</body>
</html>